package com.zkgl.rent.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zkgl.common.annotation.Log;
import com.zkgl.common.core.controller.BaseController;
import com.zkgl.common.core.domain.AjaxResult;
import com.zkgl.common.core.domain.entity.SysUser;
import com.zkgl.common.core.page.TableDataInfo;
import com.zkgl.common.enums.BusinessType;
import com.zkgl.common.utils.poi.ExcelUtil;
import com.zkgl.rent.domain.Tenant;
import com.zkgl.rent.service.ITenantService;

import jakarta.servlet.http.HttpServletResponse;

/**
 * 租客信息Controller
 * 
 * @author macro
 * @date 2025-09-25
 */
@RestController
@RequestMapping("/rent/tenant")
public class TenantController extends BaseController {
    @Autowired
    private ITenantService tenantService;

    /**
     * 查询租客信息列表
     */
    @PreAuthorize("@ss.hasPermi('rent:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tenant tenant) {
        startPage();
        List<Tenant> list = tenantService.selectTenantList(tenant);
        return getDataTable(list);
    }
    
    /**
     * 查询租客信息列表
     */
    @PreAuthorize("@ss.hasPermi('rent:tenant:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(Tenant tenant) {
        List<Tenant> list = tenantService.selectTenantList(tenant);
        return getDataTable(list);
    }

    /**
     * 导出租客信息列表
     */
    @PreAuthorize("@ss.hasPermi('rent:tenant:export')")
    @Log(title = "租客信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tenant tenant) {
        List<Tenant> list = tenantService.selectTenantList(tenant);
        ExcelUtil<Tenant> util = new ExcelUtil<Tenant>(Tenant.class);
        util.exportExcel(response, list, "租客信息数据");
    }

    /**
     * 获取租客信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('rent:tenant:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Tenant tenant = tenantService.selectTenantById(id);
        // 判断是否为本人房源
        if (!SysUser.isAdmin(getUserId())) {
            if (!getUserId().equals(tenant.getLandlordId())) {
                return error("无权查看此租客信息，如有疑问请联系管理员");
            }
        }
        return success(tenant);
    }

    /**
     * 新增租客信息
     */
    @PreAuthorize("@ss.hasPermi('rent:tenant:add')")
    @Log(title = "租客信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tenant tenant) {
        tenant.setLandlordId(getUserId());
        return toAjax(tenantService.insertTenant(tenant));
    }

    /**
     * 修改租客信息
     */
    @PreAuthorize("@ss.hasPermi('rent:tenant:edit')")
    @Log(title = "租客信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tenant tenant) {
        Tenant tenantDb = tenantService.selectTenantById(tenant.getId());
        // 判断是否为本人房源
        if (!SysUser.isAdmin(getUserId())) {
            if (!getUserId().equals(tenantDb.getLandlordId())) {
                return error("无权修改此租客信息，如有疑问请联系管理员");
            }
        }
        return toAjax(tenantService.updateTenant(tenant));
    }

    /**
     * 删除租客信息
     */
    @PreAuthorize("@ss.hasPermi('rent:tenant:remove')")
    @Log(title = "租客信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tenantService.deleteTenantByIds(ids));
    }
}
